var mongoose = require('./model');
var usersSchema = new mongoose.Schema({
    username: {type: String, unique: true},
    password: {type: String},
    name: {type: String},
    email: {type: String},
    phone: {type: Number},
    message: {type: String}
});
var usersModel = mongoose.model('user', usersSchema, 'user');
exports.login = function (user, callback) {
    if (user.username == '' || user.password == '') {
        callback(false, '用户名或密码为空');
        return false;
    }
    usersModel.find(user, function (err, doc) {
        if (err)
            throw err;
        if (doc.length > 0) {
            callback(doc)
        } else {
            callback(doc, "登录失败");
        }
    })
};
exports.register = function (user, callback) {
    usersModel.find({username: user.username}, function (err, doc) {
        if (err)
            throw err;
        if (doc.length > 0) {
            callback('fail');
        } else {
            usersModel.create(user, function (err) {
                if (err)
                    throw err;
                callback('success');
            })
        }
    });
};
exports.update=function (username,user,callback) {
    usersModel.update({username:username},{$set:user},function (err,res) {
        if (err){
            callback('fail');
        }else{
            callback('success');
        }
    })

};
exports.getUser=function (username,callback) {
    usersModel.find(username,function (err,doc) {
        if (err)
            throw err;
        if (doc.length > 0) {
            callback(doc)
        } else {
            callback(doc, "用户信息获取失败");
        }
    })
};
